package com.nercel.dsj.gksales.async;

import java.util.Date;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import com.nercel.dsj.gksales.model.ResponseResult;
import com.nercel.dsj.gksales.model.entity.SpikeRecord;
import com.nercel.dsj.gksales.service.SpikeRecordService;
import com.nercel.dsj.gksales.util.SpikeResult;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
@Aspect
public class SpikeAsync {

	@Autowired
	private SpikeRecordService spikeRecordService;

	@Pointcut(value = "execution(* com.nercel.dsj.gksales.api.controller.SpikeApiController.doSpike(java.lang.Integer,java.lang.String)) &&args(id,phone)")
	public void doSpikePointCut(Integer id, String phone) {
	}

	@Async
	@AfterReturning(value = "doSpikePointCut(id,phone)", returning = "returnValue")
	public void doSpike(Integer id, String phone, Object returnValue) {
		SpikeRecord spikeRecord = new SpikeRecord();
		spikeRecord.setSpikeId(id);
		spikeRecord.setUserPhone(phone);
		ResponseResult aj = (ResponseResult) returnValue;
		spikeRecord.setResult(SpikeResult.getCodeByMsge((String)aj.getData()));
		spikeRecord.setSpikeTime(new Date());
		spikeRecordService.save(spikeRecord);
		log.info("秒杀记录保存成功！");
	}
}
